﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.Serialization.Json;
using System.IO;
using HighSign.Common.Plugins;
using System.Drawing;
using System.Threading;
using System.Diagnostics;

namespace HighSign
{
	static class Program
	{
		#region Controls Initialization

		private static NotifyIcon TrayIcon;
		private static ContextMenuStrip	TrayMenu;
		private static ToolStripMenuItem miTrainingMode;
		private static ToolStripSeparator miSeperator1;
		private static ToolStripMenuItem miAvailableActions;
		private static ToolStripMenuItem miAvailableGestures;
		private static ToolStripMenuItem miOptions;
		private static ToolStripSeparator miSeperator2;
		private static ToolStripMenuItem miExitHighSign;

		#endregion

		#region Noficiation Icon and Tray Menu Setup

		private static void SetupTrayIconAndTrayMenu()
		{
			TrayIcon = new NotifyIcon();
			TrayMenu = new ContextMenuStrip();
			miTrainingMode = new ToolStripMenuItem();
			miSeperator1 = new ToolStripSeparator();
			miAvailableActions = new ToolStripMenuItem();
			miAvailableGestures = new ToolStripMenuItem();
			miOptions = new ToolStripMenuItem();
			miSeperator2 = new ToolStripSeparator();
			miExitHighSign = new ToolStripMenuItem();

			// Tray Icon
			TrayIcon.ContextMenuStrip = TrayMenu;
			TrayIcon.Text = "High Sign";
			TrayIcon.Visible = true;
			TrayIcon.DoubleClick += (o, e) => { ToggleTeaching(); };

			// Tray Menu
			TrayMenu.Items.AddRange(new ToolStripItem[] { miTrainingMode, miSeperator1, miAvailableActions, miAvailableGestures, miOptions, miSeperator2, miExitHighSign });
			TrayMenu.Name = "TrayMenu";
			TrayMenu.Size = new Size(194, 82);
			TrayMenu.Text = "High Sign Tray Menu";
			TrayMenu.Opened += (o, e) => { Input.MouseCapture.Instance.DisableMouseCapture(); };
			TrayMenu.Closed += (o, e) => { Input.MouseCapture.Instance.EnableMouseCapture(); };

			// Training Mode Menu Item
			miTrainingMode.Checked = true;
			miTrainingMode.CheckOnClick = true;
			miTrainingMode.CheckState = CheckState.Checked;
			miTrainingMode.Name = "TrainingModeMenuItem";
			miTrainingMode.Size = new Size(193, 22);
			miTrainingMode.Text = "Training Mode";
			miTrainingMode.Click += (o, e) => { ToggleTeaching(); };
			
			// First Seperator Menu Item
			miSeperator1.Name = "Seperator1";
			miSeperator1.Size = new Size(190, 6);
			
			// Available Actions Menu Item
			miAvailableActions.Name = "AvailableActions";
			miAvailableActions.Size = new Size(193, 22);
			miAvailableActions.Text = "Available Actions";
			miAvailableActions.Click += (o, e) => { UI.FormManager.Instance.AvailableActions.Show(); };

			// Available Gestures Menu Item
			miAvailableGestures.Name = "AvailableGestures";
			miAvailableGestures.Size = new Size(193, 22);
			miAvailableGestures.Text = "Available Gestures";
			miAvailableGestures.Click += (o, e) => { UI.FormManager.Instance.AvailableGestures.Show(); };
			
			// Options Menu Item
			miOptions.Name = "Options";
			miOptions.Size = new Size(193, 22);
			miOptions.Text = "Preferences";
			miOptions.Click += (o, e) => { UI.FormManager.Instance.Options.Show(); };

			// Second Seperator Menu Item
			miSeperator2.Name = "Seperator2";
			miSeperator2.Size = new Size(190, 6);
			
			// Exit High Sign Menu Item
			miExitHighSign.Name = "ExitHighSign";
			miExitHighSign.Size = new Size(193, 22);
			miExitHighSign.Text = "Exit High Sign";
			miExitHighSign.Click += (o, e) =>
			{
				// Uninstall mouse hook and exit program
				TrayIcon.Visible = false;
				Input.MouseCapture.Instance.DestroyMouseHook();
				Application.ExitThread();
			};
		}

		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>  
		[STAThread]
		static void Main()
		{
			bool createdNew = true;
			using (Mutex mutex = new Mutex(true, "HighSign", out createdNew))
			{
				if (createdNew)
				{
					Application.EnableVisualStyles();
					Application.SetCompatibleTextRenderingDefault(false);

					Input.MouseCapture.Instance.Load();
					Gestures.GestureManager.Instance.Load();
					UI.FormManager.Instance.Load();
					//Drawing.Compatibility.Surface.Instance.Load();
					Applications.ApplicationManager.Instance.Load();
					Plugins.PluginManager.Instance.Load();

					SetupTrayIconAndTrayMenu();

					EnterUserDefinedMode();

					Application.Run();
				}
			}
		}

		#region Public Methods

		public static void EnterUserDefinedMode()
		{
			switch ((Configuration.StartupMode)Properties.Settings.Default.StartupMode)
			{
				case Configuration.StartupMode.Training:
					StartTeaching();
					break;
				case Configuration.StartupMode.Gesture:
					StopTeaching();
					break;
				case Configuration.StartupMode.LastInstance:
					if (Properties.Settings.Default.Teaching)
						StartTeaching();
					else
						StopTeaching();
					break;
			}
		}

		public static void ToggleTeaching()
		{
			// Toggle teaching mode
			if (Properties.Settings.Default.Teaching)
				StopTeaching();
			else
				StartTeaching();
		}

		public static void StartTeaching()
		{
			Properties.Settings.Default.Teaching = miTrainingMode.Checked = true;
			Properties.Settings.Default.Save();

			// Assign resource icon as tray icon
			TrayIcon.Icon = Icon.FromHandle(HighSign.Properties.Resources.MouseIconTraining.GetHicon());
		}

		public static void StopTeaching()
		{
			Properties.Settings.Default.Teaching = miTrainingMode.Checked = false;
			Properties.Settings.Default.Save();

			// Assign resource icon as tray icon
			TrayIcon.Icon = Icon.FromHandle(HighSign.Properties.Resources.MouseIcon.GetHicon());
		}

		#endregion
	}
}
